package cn.usts.edu.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ServletController {

    // 返回内容
    @RequestMapping("/s1")
    public void servletController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.getWriter().println("servlet is released");

    }

    // 返回指定的页面
    // 【没有视图解析器   加上也访问不到/WEB-INF/jsp 除非跳转or重定向】
    // 【有return的需要视图解析器】
    @RequestMapping("/s2")
    public String servletController2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return "/servlet";
    }

    // 请求重定向的页面
    @RequestMapping("/s3")
    public void servletController3(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // /WEB-INF/下的目录无法访问   【地址会变】
        response.sendRedirect("/sendRedirect.jsp");
    }

    // 请求转发
    @RequestMapping("/s4")
    public void servletController4(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // /WEB-INF/下的目录无法访问    forward(request,response)携带参数转发   【地址不会变】
        request.setAttribute("msg","requestDispatcher released");
        request.getRequestDispatcher("/WEB-INF/jsp/requestDispatch.jsp").forward(request,response);
    }


}